from apps.common.base.views import *
from apps.system.router.serializers import *
from apps.system.router.dao import RouterDao
from apps.system.router.filters import RouterFilter

class BaseRouterViewSet(BaseViewSet):
    serializer_class = RouterModelSerializer
    filter_class = RouterFilter
    DAO_CLASS = RouterDao

    @action(methods=['post'], detail=False)
    def updateAction(self, request):
        params = getRequestParams(self.request)
        obj_id = params.get('obj_id')
        if isEmptyStr(obj_id):
            return Response(response_message('obj_id参数必填'))
        if params.get('hidden'):
            params['hidden'] = 1
        else:
            params['hidden'] = 0
        self.DAO_CLASS().update(obj_id, params)
        return Response(response_success('更新成功'))

    @action(methods=['post'], detail=False)
    def saveAction(self, request):
        params = getRequestParams(self.request)
        obj_id = params.get('id')
        if obj_id:
            self.DAO_CLASS().update(obj_id, params)
        else:
            if params.get('is_root'):
                params['component'] = 'Layout'
            self.DAO_CLASS().create(params)
        return Response(response_success('保存成功'))